﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using BLLMagiPack.Managers;
using ModeloMagiPack.Entidades;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace MaGiPack
{
    public delegate void LanzarForm();

    public class Utilidades
    {
        public static CultureInfo Cultura 
        {
            get
            {
                CultureInfo inf = (CultureInfo)System.Windows.Forms.Application.CurrentCulture.Clone();
                inf.NumberFormat.NumberDecimalSeparator = ".";
                inf.NumberFormat.NumberGroupSeparator = ",";

                return inf;
            }
        }

        public static int IVA
        {
            get
            {
                int iva = 21;

                if (ConfigurationSettings.AppSettings["IVAPorDefecto"] != null)
                    iva = int.Parse(ConfigurationSettings.AppSettings["IVAPorDefecto"]);

                return iva;
            }
        }

        public static void TextboxSoloNumeros_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !"0123456789,.".Contains(e.KeyChar.ToString()) && ((int) e.KeyChar) != 8;
        }

        public static void TextboxSoloEnteros_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !"0123456789".Contains(e.KeyChar.ToString()) && ((int)e.KeyChar) != 8;
        }

        public static void BloquearTextbox(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        public static bool ValidarDatos(Control padre, ErrorProvider error)
        {
            bool ok = true;

            foreach (Control control in padre.Controls)
            {
                TextBox txt = control as TextBox;

                if (txt != null && txt.Tag != null && txt.Tag.Equals("Requerido") && txt.TextLength == 0)
                {
                    error.SetError(txt, "Requerido");
                    ok = false;
                }

                ComboBox cbo = control as ComboBox;

                if(cbo != null && cbo.Tag != null && cbo.Tag.Equals("Requerido") && cbo.SelectedIndex == 0)
                {
                    error.SetError(cbo, "Seleccione una opción");
                    ok = false;
                }

                MaskedTextBox mTxt = control as MaskedTextBox;

                if (mTxt != null && mTxt.Tag != null && mTxt.Tag.Equals("Requerido") && !mTxt.MaskFull)
                {
                    error.SetError(mTxt, "Requerido");
                    ok = false;
                }

                if (ok)
                    ok = ValidarDatos(control, error);
                else
                    ValidarDatos(control, error);
            }

            return ok;
        }

        public static void LimpiarErrores(Control padre, ErrorProvider errorProvider)
        {
            foreach (Control control in padre.Controls)
            {
                errorProvider.SetError(control, "");
                LimpiarErrores(control, errorProvider);
            }
        }

        public static void LimpiarControles(Control padre)
        {
            foreach (Control control in padre.Controls)
            {
                TextBox txt = control as TextBox;

                if (txt != null)
                {
                    txt.Text = string.Empty;
                    continue;
                }

                ComboBox cbo = control as ComboBox;

                if (cbo != null)
                {
                    if(cbo.Items.Count > 0)
                        cbo.SelectedIndex = 0;

                    continue;
                }

                CheckBox chk = control as CheckBox;

                if (chk != null)
                {
                    chk.Checked = false;
                    continue;
                }

                MaskedTextBox mTxt = control as MaskedTextBox;

                if (mTxt != null)
                {
                    mTxt.Clear();
                    continue;
                }

                LimpiarControles(control);
            }
        }

        /// <summary>
        /// Método hecho para simplificar el lanzamiento de los forms de ABM
        /// </summary>
        /// <typeparam name="Formulario">Tipo del formulario que realiza el ABM</typeparam>
        /// <param name="cbo">combobox de este formulario utilizado para la selección</param>
        /// <param name="lanzar">Método que realiza la carga de elementos</param>
        public static void LanzarAbm<Formulario>(ComboBox cbo, LanzarForm lanzar) where Formulario : Form
        {
            Formulario frm = Activator.CreateInstance<Formulario>();
            frm.ShowDialog();

            int selectedIndex = cbo.SelectedIndex;
            lanzar();

            if (selectedIndex > -1 && selectedIndex < cbo.Items.Count)
                cbo.SelectedIndex = selectedIndex;
        }

        public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
    }
}
